package fy.mdm.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.message.impl.MsgObject;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;

import fy.config.FyConfig;
import fy.mdm.bean.NewCustomerCodeBean;
import fy.util.DBUtil;
public class NewCustomerCodeDao {
	private static Log log = LogFactory.getLog(NewCustomerCodeDao.class);
	private NewCustomerCodeBean NewCustomerCodeBean;
	//写入MDM数据库
	public boolean getStateReturn() {
		try {
			
				IServiceRequester Requester = BaseServiceRequester.getInstance();
				List<GroupRecord> grs1 = new ArrayList<GroupRecord>();//多层用
				// 定义请求报文对象
				IMsgObject reqMo = new MsgObject(IMsgObject.MOType.initSR);
				String SourceSysID = "06004"; // 请求方(发布方)
				String ServiceID = "06001000000043";// 响应方(发布方)
				// 获取系统时间
				Date date = new Date();
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
				String format = simpleDateFormat.format(date);
				// 对请求报文对象进行设置
				reqMo.setSourceSysID(SourceSysID); // 设置报文 请求方系统号
				reqMo.setSerialNO(format.substring(0, 8) + SourceSysID
						+ String.format("%06d", ++MaterialRequestNoTimerTask.requestNo));// 设置报文流水号,第三段表示今天发送的第几的一次报文
				reqMo.setServiceID(ServiceID); // 设置报文 提供方服务号
				reqMo.setServiceDateTime(format); // 设置报文 日期时间
				reqMo.setReqValue("BOID","SOA_MDM_CUSTOMER_OTHER_F" );
				reqMo.setReqValue("ControlType", "5");
				reqMo.setReqValue("REMARK","OA");
				GroupRecord gr1 = new GroupRecord();
				gr1.setName("ROW");
				gr1.setFieldValue("CREATE_TIME",NewCustomerCodeBean.getCreatetime());//发起日期
				gr1.setFieldValue("CUSTOMER_CODE",NewCustomerCodeBean.getCustomerCode());//编码
				gr1.setFieldValue("CUSTOMER_NAME",NewCustomerCodeBean.getCustomerName());//名称
				gr1.setFieldValue("CUSTOMER_STYLE",getEnumValue("客户",NewCustomerCodeBean.getCustomerstype()));//类型2768576876223220004
				gr1.setFieldValue("CREATOR",NewCustomerCodeBean.getCreator());//申请人
				gr1.setFieldValue("APPROVER",NewCustomerCodeBean.getApprover());//会计
				gr1.setFieldValue("COUNTRY",getEnumValue("客户国别",NewCustomerCodeBean.getCountry()));//国别-6451029771514458423
				gr1.setFieldValue("APPROVER_TIME",NewCustomerCodeBean.getApprovertime());//审批日期
			    gr1.setFieldValue("CUSTOMER_SHORT_NAME","");
			    gr1.setFieldValue("DAYS","");
			gr1.setFieldValue("AREA","");
			gr1.setFieldValue("TAX_REGISTRATION_NUMBER","");
			gr1.setFieldValue("ADDRESS","");
			gr1.setFieldValue("CUSTOMER_CATEGORY","");
			gr1.setFieldValue("PAYMENT_CLAUSE","");
			gr1.setFieldValue("PRICE_LIST","");
			gr1.setFieldValue("LEGAL_REPRESENTATIVE","");
			gr1.setFieldValue("LEGAL_REPRESENTATIVE_PHONE","");
			gr1.setFieldValue("NAME","");
			gr1.setFieldValue("PHONE","");
			gr1.setFieldValue("PROVINCE","");
			gr1.setFieldValue("CITY","");
			gr1.setFieldValue("ADDRESS1","");
			//log.info("ADDRESS1="+this.partNewCustomerBean.getADDRESS1());
			gr1.setFieldValue("ADDRESS2","");
			gr1.setFieldValue("ADDRESS3","");
			gr1.setFieldValue("STATUS","ACTIVE");
			    gr1.setFieldValue("TRANSACTION_TYPE","1");
				grs1.add(gr1);
				reqMo.setReqGroupRecord(grs1);
				log.info("reqMo:"+reqMo);
				IMsgObject respMo = null;
				respMo = Requester.execute(reqMo);//发布报文
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return true;
		
	}
	public NewCustomerCodeBean getNewCustomerCodeBean() {
		return NewCustomerCodeBean;
	}
	public void setNewCustomerCodeBean(NewCustomerCodeBean newCustomerCodeBean) {
		NewCustomerCodeBean = newCustomerCodeBean;
	}
	//查询下拉值
	public String getUnitCode(String code, String temp) {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;

		try {
			conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
			pst = conn.prepareStatement("SELECT c.SHOWVALUE FROM  CTP_ENUM_ITEM  c WHERE c.ENUMVALUE=? AND c.ENUMNAME=?");
			pst.setString(1, code);
			pst.setString(2, temp);
			rs = pst.executeQuery();
			if (rs.next()) {
				code = rs.getString(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, pst);
		}
		return code;
	}
	public String getEnumValue(String enumName,String value)throws Exception{
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
		conn.setAutoCommit(false);
		pst = conn.prepareStatement("select l.SHOWVALUE from CTP_ENUM t left join CTP_ENUM_ITEM l on t.ID = l.REF_ENUMID WHERE l.ENUMVALUE = ? AND t.ENUMNAME = ?");
		pst.setString(1,value);
		pst.setString(2,enumName);
		rs = pst.executeQuery();
		while (rs.next()) {
			log.info("getEnumValue = "+rs.getString(1));
			return rs.getString(1);
		}
		DBUtil.close(rs);
		return "";
	}
}
